<? 
	include('base.php');
	User::protect();
	include_class('releases');
	include_class('release_tracks');
	include_class('m2');
	$section = 'releases';
	$rt = ReleaseTrack::get($_GET['id']);
	if (!db::isError($rt)) {
		$r = $rt->getReleaseObject();
		
		$doSubmit = false;
		if (!$_POST['area']) {
			$ma = $rt->getAVAreaObject();
		} else {			
			if ($_POST['area'] == $rt->getAVAreaID()) {
				$doSubmit = true;
			}
		}
		
		if ($doSubmit) {
			$ma = MediaArea::get($_POST['area']);
			if (!db::isError($ma)) {
			
				if ($_POST['localfile']) {
					$res = $ma->addMediaUpload('mediafile', $rt);
				} else if ($_POST['url']) {
					$res = $ma->addMediaRemote($_POST['url'], $rt);
				} else if ($_POST['toAdd'] == 'all') { 
					$keepCopy = ($_POST['copyFromIncoming']) ? 1 : 0;
					$res = $ma->addAllMediaIncoming($keepCopy, $rt);
				} else if ($_POST['incoming_file']) {
					$keepCopy = ($_POST['copyFromIncoming']) ? 1 : 0;
					$res = $ma->addMediaIncoming($_POST['incoming_file'], $keepCopy, $rt);
				}
			}
			
			if (!db::isError($res) && $_POST['toAdd'] == 'all') {
				header('Location: release_track_media.php?id=' . $_GET['id']);
				exit;
			} else if (!db::isError($res)) {
				header('Location: release_track_media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $res);
				exit;
			}
		}
	}	
	$page_title = 'Add Release Track Media';
	include('layout/header.php'); 
	
?>

<? if (!db::isError($rt) && !db::isError($r)) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="releases.php">Releases &#62;</a>
	<a href="release_edit.php?id=<?=$r->getID()?>">View/Edit Release &#62;</a>
	<a href="release_track_edit.php?id=<?=$rt->getID()?>">View/Edit Release Track &#62;</a>
	<a href="release_track_media.php?id=<?=$_GET['id']?>">Media&nbsp;&#62;</a>
	Add Media
</div>

<?
	$canAdd = User::isAdmin();
	if ($res) { // it's an error
		if (db::isError($res)) {
			$res->outputList();
		}
	}
	if (!db::isError($ma)) { 
		if ($canAdd) { 
			include('layout/media_add.php');
		} else {
			Error::outputDialog('Return to Release Track','release_track_edit.php?id=' . $_GET['id'], 'You are not allowed to add media to a track.');
		}
	} else {
		$ma->outputDialog('Return to Track','release_track_edit.php?id=' . $_GET['id']);
	}

} else { ?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href=".php">Manage&nbsp;Band&nbsp;&#62;</a>&nbsp;<a href="releases.php">Releases&nbsp;&#62;</a>&nbsp;Invalid Entry
</div>

<? $sh->outputDialog('Return to Releases','releases.php'); ?>
	
<? } ?>

<? include('layout/footer.php'); ?>